package fr.thomascoffin.mocaf;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * This is a simple example of how the EasyGBC is used to produce more readable GridBagConstraints-using code.
 * <p/>
 * <p/>
 * This class is released by Thomas Coffin (thomas.coffin@gmail.com) under the <a href="http://www.gnu.org/copyleft/lesser.html" target="_blank">LGPL License</a>
 * as a component of the <a href="http://code.google.com/p/mocaf" target="_blank">mocaf project</a>
 * <p/>
 * (c) Thomas Coffin 2008.
 */
public class EasyGBCSimpleExample extends JFrame implements ActionListener
{
	private static final String CANCEL = "Cancel";
	private static final String CLEAR = "Clear";
	private static final String OK = "OK";
	private final JTextArea _textArea;

	public static void main(String[] args)
	{
		new EasyGBCSimpleExample();
	}

	private EasyGBCSimpleExample()
	{
		super("EasyGBC Simple Example");
		JButton cancelButton = createButton(CANCEL);
		JButton clearButton = createButton(CLEAR);
		JButton okButton = createButton(OK);

		_textArea = new JTextArea("This is an example for the EasyGBC class,\n" +
			"which extends the GridBagConstraints in a more developer-friendly way\n" +
			"Hope this helps...\n");

		JPanel panel = new JPanel(new GridBagLayout());
		EasyGBC gbc = new EasyGBC(getGuiDefaultConstraints());
		panel.add(_textArea, gbc.fillBoth().width(3));
		EasyGBC buttonGBC = gbc.noFill().width(1).insets(2).goDown().weightY(0);
		panel.add(cancelButton, buttonGBC.east());
		panel.add(clearButton, buttonGBC.goRight().center());
		panel.add(okButton, buttonGBC.goRight().west());

		getContentPane().add(panel);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	private JButton createButton(final String text)
	{
		JButton cancelButton = new JButton(text);
		cancelButton.setActionCommand(text);
		cancelButton.addActionListener(this);
		return cancelButton;
	}

	private static GridBagConstraints getGuiDefaultConstraints()
	{
		return new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
	}

	public void actionPerformed(ActionEvent e)
	{
		String actionCommand = e.getActionCommand();
		if (CANCEL.equals(actionCommand) || OK.equals(actionCommand))
		{
			setVisible(false);
			dispose();
		}
		else if (CLEAR.equals(actionCommand))
		{
			_textArea.setText("");
		}
	}
}
